package com.xu.commons.dao;

import java.util.Properties;





import com.xu.commons.until.PropsUtil;
import com.xu.commons.until.StringUtil;



/**
 * 生成sql
 * @author xupeijiao
 *
 */
public class Sqlhelper {
	
	  /**
     * SQL 属性文件对象
     */
    private static final Properties sqlProps = PropsUtil.loadProps("sql.properties");
    public static String getSql(String key){
    	String sql;
    	if(sqlProps.containsKey(key)){
    		sql = sqlProps.getProperty(key);
    	}else{
    		throw new RuntimeException("无法在配置文件里获取属性");
    	}
    	return sql;
    }

    
    public static String createSelectSql(String tableName , String condition, String sort){
    	StringBuilder sql =new StringBuilder("select * from ").append(tableName);
    	sql.append(generateWhere(condition));
    	sql.append(generateOrder(sort));
    	return sql.toString();
    }
    
    private static String generateWhere(String condition){
        String where = "";
        if (StringUtil.isNotEmpty(condition)) {
            where += " where " + condition;
        }
        return where;
    }
    private static String generateOrder(String sort) {
        String order = "";
        if (StringUtil.isNotEmpty(sort)) {
            order += " order by " + sort;
        }
        return order;
    }
}
